package com.wang.gulimall.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wang.common.utils.PageUtils;
import com.wang.common.utils.Query;
import com.wang.common.utils.R;
import com.wang.gulimall.ware.dao.WareInfoDao;
import com.wang.gulimall.ware.entity.WareInfoEntity;
import com.wang.gulimall.ware.feign.MemberFeignService;
import com.wang.gulimall.ware.service.WareInfoService;
import com.wang.gulimall.ware.vo.FareVo;
import com.wang.gulimall.ware.vo.MemberAddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Map;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Autowired
    private MemberFeignService memberFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<WareInfoEntity> wareInfoEntityQueryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            wareInfoEntityQueryWrapper.eq("id", key).or()
                    .like("name", key)
                    .or().like("address", key)
                    .or().like("areacode", key);
        }

        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                wareInfoEntityQueryWrapper
        );

        return new PageUtils(page);
    }

    /**
     * 根据收货地址计算运费
     *
     * @param addrId
     * @return
     */
    @Override
    public BigDecimal getAddressFare(Long addrId) {
        ObjectMapper mapper = new ObjectMapper();
        // 远程查询当前地址信息
        //R res = memberFeignService.info(addrId);
//        Object object = res.get("memberReceiveAddress");
//        MemberAddressVo address = mapper.convertValue(object, MemberAddressVo.class);
//        if (address != null) {
//            String substring = address.getPostCode().substring(0, 2);
//            return new BigDecimal(substring);
//        }
        return new BigDecimal(12);
    }

    /**
     * 计算运费
     * @param addrId
     * @return
     */
    @Override
    public FareVo getFare(Long addrId) {

        FareVo fareVo = new FareVo();

        //收获地址的详细信息
        R addrInfo = memberFeignService.info(addrId);

        MemberAddressVo memberAddressVo = addrInfo.getData("memberReceiveAddress",new TypeReference<MemberAddressVo>() {});

        if (memberAddressVo != null) {
            String phone = memberAddressVo.getPhone();
            //截取用户手机号码最后一位作为我们的运费计算
            //1558022051
            String fare = phone.substring(phone.length() - 10, phone.length()-8);
            BigDecimal bigDecimal = new BigDecimal(fare);

            fareVo.setFare(bigDecimal);
            fareVo.setAddress(memberAddressVo);

            // 地址 +运费
            return fareVo;
        }
        return null;
    }
}